package project;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;

/**
 * Represents a user and also has some static methods for loading
 * existing users and setting active user.
 */
public class User
{
	// In this source code, all the static members that manages loading and selecting of users are arranged first:
	
	/**
	 * A set of existing users. Loaded with the static method loadUsers. 
	 */
	private static ArrayList<User> users = new ArrayList<User>();
	
	/**
	 * The index of the current active user. -1 if not set.
	 */
	private static int activeUser = -1;
	
	/**
	 * Just a boolean value if the users data file is loaded.
	 */
	private static boolean usersLoaded = false;
	
	/**
	 * User database file:
	 */
	private static File userFile = new File("./Users.dat");
	
	/**
	 * Loads the existing users from a datafile. Should be called in every
	 * static method that uses the ArrayList users.
	 * 
	 * TODO: This funcion is ment to load existing users
	 * 		 from a datafile. As by now, it simply creates
	 * 		 some hard coded example users.
	 * @return 
	 */
	private static void loadUsers()
	{
		if (usersLoaded) {
			return;
		}
		
		// Create username file:
		if (!userFile.exists()) {
			System.out.println("Creating user database");
			try {
				userFile.createNewFile();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(V12.getInstance().mFrame, "Kunne ikke opprette datafil for brukerdatabase. Brukerne du oppretter vil ikke bli lagret.");
			}
		}
		
		// Read username file:
		try {
			Scanner in = new Scanner(userFile);
		
			while (in.hasNext()) {
				String username = in.nextLine().trim();
				if (username.length() < 1) {
					continue;
				}
				
				User user = new User();
				user.setUsername(username);
				users.add(user);
			}
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		usersLoaded = true;
	}
	
	/**
	 * Adds a new user to the user database.
	 * @param user
	 * 
	 * TODO: Should save a nicer file with not so many empty spaces.
	 */
	public static void addUser(User user)
	{
		loadUsers();
		users.add(user);
		
		try {
			FileWriter out = new FileWriter(userFile, true);
			out.write("\r\n" + user.getUsername() + "\r\n");
			out.close();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(V12.getInstance().mFrame, "Kunne ikke lagre til brukerdatabasen. Brukeren du har opprettet vil ikke eksistere neste gang du bruker denne applikasjonen.");
		}		
	}

	/**
	 * Returns a list of all available users.
	 * @return
	 */
	public static List<User> getUsers()
	{
		loadUsers();
		return users;
	}
	
	/**
	 * Sets the active user by username. Returns boolean true if success.
	 * 
	 * @param username
	 */
	public static boolean setActiveUser(String username)
	{
		if (username == null) {
			activeUser = -1;
		}
		
		loadUsers();
			
		for (int i = 0; i < users.size(); i++) {
			if (users.get(i).getUsername().equals(username)) {
				activeUser = i;
				return true;
			}
		}
		
		return false; 
	}
	
	/*
	 * Returns the active user.
	 * 
	 * @return
	 */
	public static User getActiveUser()
	{
		loadUsers();
		
		if (activeUser == -1) {
			return null;
		}
		
		return users.get(activeUser);
	}
	
	
	
	
	
	/**
	 * Username
	 */
	protected String username;
	
	
	/**
	 * TODO:
	 */
	public User() {
		username = "Dummy User";
	}
	
	/**
	 * Returns the user name
	 */
	public String getUsername() { return username; }
	
	/**
	 * Sets the user name.
	 * @param username
	 */
	public void setUsername(String username) { this.username = username; }
}
